﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Services;
using PM.Entity;

namespace PM.Site.Admin
{
    public partial class ClientForm : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                
                    this.Loading();
                    if (Session["ClientId"] != null && Session["ClientId"].ToString() != string.Empty)
                    {                        
                        this.Loading(Session["ClientId"].ToString());                        
                    }               
            }
            else
            {
                Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
        }

        private void Loading(string client_id)
        {
            if (!Page.IsPostBack)
            {
            Client client = ClientServices.GetClientByID(client_id);
            txbClientId.Text = client.ClientId;
            txbCDS.Text = client.CDS;
            txbClientId.ReadOnly = true;
            ddlTitleList.SelectedValue = client.Title;
            txbInitials.Text = client.Initials;
            txbFamilyName.Text = client.FamilyName;
            txbOtherNames.Text = client.Other_Names;
            txbSurname.Text = client.Surname;
            txbDob.Text = client.DoB.ToShortDateString();
            txbCountryOfRes.Text = client.CountryOfRes;
            txbCitizenship.Text = client.Citizenship;
            txbBankAccNo.Text = client.BankAccNo;
            ddlGender.SelectedValue = client.Gender;
            ddlMaritalStatus.SelectedValue = client.MaritalStatus;
            txbNoOfJointInvestors.Text = client.NoOfJointInvestors.ToString();
            if (client.NoOfJointInvestors > 0)
            {
                lnkJointInvestorsDetails.Visible = true;
                
            }
            txbAddRes.Text = client.AddressRes;
            txbAddOffice.Text = client.AddressOff;
            txbTelMobile.Text = client.TeleMob;
            txbTelRes.Text = client.TeleRes;
            txbTelOffice.Text = client.TeleOff;
            txbFax.Text = client.Fax;
            txbEmail.Text = client.Email;
            txbOccupation.Text = client.Occupation;
            ddlAdvisor.SelectedValue = client.AdvisorId;
            if (client.SpouseId != string.Empty)
            {
                txbSpouseName.Text = SpouseServices.GetSpouseNameByIds(client.ClientId, client.SpouseId);
                if (txbSpouseName.Text != null)
                {
                    txbSpouseName.Visible = true;
                }
            }
            txbSpouseName.ReadOnly = true;
            txbRegDate.Text = client.RegDate.ToShortDateString();
            txbRemarks.Text = client.Remarks;
            chkbActive.Checked = client.Active;
            lnkSpouseDetails.Visible = true;
            }
            
        }


        private void Loading()
        {
            if (!Page.IsPostBack)
            {
                
                ddlTitleList.DataSource = PersonUtil.GetTitleCollection();
                ddlTitleList.DataBind();
                ddlGender.DataSource = PersonUtil.GetGenderCollection();
                ddlGender.DataBind();
                //ddlAdvisor.DataSource = AdvisorServices.GetAdvisorCollection();
                //ddlAdvisor.DataBind();
                ddlMaritalStatus.DataSource = PersonUtil.GetMartialStatusCollection();
                ddlMaritalStatus.DataBind();

                ddlAdvisor.DataSource = AdvisorServices.Get_Advisor_ID_Name_Collection();
                ddlAdvisor.AppendDataBoundItems = true;
                ddlAdvisor.DataBind();
                
            }
        
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Client client = new Client();
            client.ClientId = txbClientId.Text;
            client.CDS = txbCDS.Text;
            client.BankAccNo = txbBankAccNo.Text;
            client.Title = ddlTitleList.SelectedValue;
            client.Initials = txbInitials.Text;
            client.Other_Names = txbOtherNames.Text;
            client.FamilyName = txbFamilyName.Text;
            client.Surname = txbSurname.Text;
            client.CountryOfRes = txbCountryOfRes.Text;
            client.Citizenship = txbCitizenship.Text;
            DateTime dob = DateTime.MinValue;
            if (DateTime.TryParse(txbDob.Text, out dob))
            {
                client.DoB = dob;
            }
            client.Gender = ddlGender.SelectedValue;
            client.MaritalStatus = ddlMaritalStatus.SelectedValue;
            int noOfJoints = 0;
            int.TryParse(txbNoOfJointInvestors.Text, out noOfJoints);
            client.NoOfJointInvestors = noOfJoints;
            client.AddressRes = txbAddRes.Text;
            client.AddressOff = txbAddOffice.Text;
            client.TeleMob = txbTelMobile.Text;
            client.TeleRes = txbTelRes.Text;
            client.TeleOff = txbTelOffice.Text;
            client.Fax = txbFax.Text;
            client.Email = txbEmail.Text;
            client.Occupation = txbOccupation.Text;
            client.AdvisorId = ddlAdvisor.SelectedValue;
            DateTime regDate = DateTime.MinValue;
            if (DateTime.TryParse(txbRegDate.Text, out regDate))
            {
                client.RegDate = regDate;
            }
            client.Remarks = txbRemarks.Text;
            client.Active = chkbActive.Checked;

            if (Session["ClientId"] != null && Session["ClientId"].ToString().Equals(client.ClientId))
            {
                ClientServices.Update(client);
            }
            else
            {
                ClientServices.Add(client);
            }
            lnkSpouseDetails.Visible = true;
            if (client.NoOfJointInvestors > 0)
            {
                lnkJointInvestorsDetails.Visible = true;
                
            }
            Response.Redirect("~/Admin/ClientsOverview.aspx");
        }
    }
}
